<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="代理" prop="parentName" v-if="checkPermi(['platop'])">
        <el-input
          v-model="queryParams.parentName"
          placeholder="输入代理名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户号" prop="mchNo" v-if="checkPermi(['platop'])">
        <el-input
          v-model="queryParams.mchNo"
          placeholder="输入商户号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户名" prop="mchName" v-if="checkPermi(['platop'])">
        <el-input
          v-model="queryParams.mchName"
          placeholder="输入商户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          clearable
          v-model="queryParams.state"
          dataForm
          size="small"
          placeholder="订单状态"
          class="filter-item"
          style="width: 140px"
        >
          <el-option
            v-for="item in stateArr"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="chargeList" @selection-change="handleSelectionChange">
      <el-table-column v-if="checkPermi(['platop'])" label="上游订单号" align="center" prop="serverOrderId"/>
      <el-table-column v-if="checkPermi(['platop'])" label="代理" align="center" prop="parentName"/>
      <el-table-column v-if="checkPermi(['platop'])" label="客户名" align="center" width="250">
        <template slot-scope="scope">
          <p>
            <span style="color: rgb(114, 158, 213); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">商户号</span>
            <span>{{ scope.row.mchNo }}</span>
          </p>
          <p>
            <span
              style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">名称</span>
            <span>{{ scope.row.mchName }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="时间" align="center" width="220">
        <template slot-scope="scope">
          <p>
            <span style="color: rgb(114, 158, 213); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">录入</span>
            <span>{{ scope.row.createTime }}</span>
          </p>
          <p v-if="scope.row.finishTime">
            <span
              style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">完成</span>
            <span>{{ scope.row.finishTime }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="付款人" align="center" prop="payerName"/>
      <el-table-column label="付款账号" align="center" prop="payerAccount"/>
      <el-table-column label="付款银行" align="center" prop="payerBank"/>
      <el-table-column label="手续费" align="center" prop="fee"/>
      <el-table-column label="金额" align="center" >
        <template slot-scope="scope">
          <p>
            <span>{{ scope.row.amount }}</span>
          </p>
          <p >
            <span
              style="color: rgb(86, 207, 86); border-radius: 1px; padding: 3px 7px; height: auto; width: auto;">{{ scope.row.currency }}</span>
          </p>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.state == 4" round>已入账</el-tag>
          <el-tag type="danger" v-if="scope.row.state == 3" round>审核驳回</el-tag>
          <el-tag type="primary" v-if="scope.row.state == 2" round>银行审核中</el-tag>
          <el-tag type="primary" v-if="scope.row.state == 1" round>待审核</el-tag>
          <el-tag type="primary" v-if="scope.row.state == 0" round>等待提交材料</el-tag>
          <el-tag type="danger" v-if="scope.row.state == -1" round>录入中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row.id)"
            v-if="!checkPermi(['platop']) && (scope.row.state == 0 || scope.row.state == 3)"
          >补充材料
          </el-button>
          <div v-if="checkPermi(['platop'])" style="display: flex;flex-direction: column">
            <el-button
              style="margin:auto;margin-bottom: 10px;width: 100px"
              size="mini"
              type="primary"
              icon="el-icon-search"
              @click="downloadChargeZip(scope.row)"
              v-if="scope.row.state == 1"
            >下载材料
            </el-button>
            <el-button
              style="margin:auto;margin-bottom: 10px;width: 100px"
              size="mini"
              type="primary"
              @click="audit(scope.row.id, true)"
              icon="el-icon-check"
              v-if="scope.row.state == 1"
            >提交贝森
            </el-button>
            <el-button
              style="margin:auto;width: 100px"
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="audit(scope.row.id, false)"
              v-if="scope.row.state == 1"
            >审核驳回
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <div style="width: 100%;text-align: center">
        <el-upload
          class="upload-demo"
          drag
          :before-upload="handleBeforeUpload"
          :headers="headers"
          ref="upload"
          :action="uploadFileUrl"
          :on-error="uploadFail"
          :on-success="uploadSuccess"
          :file-list="fileList"
        >
          <i class="el-icon-upload"></i>
          <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          <div class="el-upload__tip" slot="tip">不超过10MB</div>
        </el-upload>
      </div>

      <div slot="footer" class="dialog-footer" style="text-align: right;">
        <el-button type="primary" @click="submitAudit">提交审核</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {listCharge} from "@/api/fusion/va/charge";
import {submitChargeAudit, submitToBison} from "@/api/fusion/va/module/bison";
import { getToken } from "@/utils/auth";
export default {
  name: "Charge",
  data() {
    return {
      uploadFileUrl: process.env.VUE_APP_BASE_API  + "/bison/uploadChargeFile",
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      stateArr: [
        {label: '录入中', value: -1},
        {label: '待补充材料', value: 0},
        {label: '待审核', value: 1},
        {label: '银行审核中', value: 2},
        {label: '审核驳回', value: 3},
        {label: '已入账', value: 4}
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      fileList:[],
      // VA充值表格数据
      chargeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        isAsc: 'desc',
        orderByColumn: 'id',
        state: '',
        pageNum: 1,
        pageSize: 10,
        provider: 'bison'
      },
      targetOrderId: undefined,
      // 表单参数
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询VA充值列表 */
    getList() {
      this.loading = true;
      listCharge(this.queryParams).then(response => {
        this.chargeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleBeforeUpload() {
      return new Promise((resolve) => {
        let that = this
        this.$nextTick(() => {
          that.uploadFileUrl = process.env.VUE_APP_BASE_API  + "/bison/uploadChargeFile/" + that.targetOrderId;
          resolve();
        })
      })
    },
    uploadFail(response) {

    },
    uploadSuccess(response) {
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mchNo: null,
        mchName: null,
        parentNo: null,
        parentName: null,
        state: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.provider = 'bison'
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {}
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加VA充值";
    },
    audit(id, auditResult) {
      this.$modal.confirm(auditResult?'是否提交到贝森?' :'是否驳回?').then(() => {
        submitToBison(id, auditResult).then(response =>{
          this.$modal.msgSuccess(response.msg)
          this.getList()
        })
      }).catch(() => {
      });
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      this.reset();
      this.fileList = []
      this.open = true;
      this.targetOrderId = id
      this.title = "提交充值证明材料";
    },
    /** 提交按钮 */
    submitAudit() {
      this.$modal.confirm('是否确认提交审核?').then(() => {
        submitChargeAudit(this.targetOrderId).then(response =>{
          this.$modal.msgSuccess(response.msg)
          this.open = false;
          this.getList()
        })
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除VA充值编号为"' + ids + '"的数据项？').then(function () {
        return delCharge(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    downloadChargeZip(order) {
      this.download('bison/downloadChargeZip/' + order.id, {
        ...this.queryParams
      }, `充值补充材料_${order.id}.zip`)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('va/charge/export', {
        ...this.queryParams
      }, `charge_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
